package com.dreamchaser.familydevice.shiro.realm;


import com.dreamchaser.familydevice.dao.UserMapper;
import com.dreamchaser.familydevice.shiro.token.JwtToken;
import org.apache.shiro.realm.Realm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author
 * @Description realm管理器
 */
@Component
public class RealmManager {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserMapper userMapper;
//    private JwtMatcher jwtMatcher;
//
//    @Autowired
//    public RealmManager(JwtMatcher jwtMatcher) {
//        this.jwtMatcher = jwtMatcher;
//    }

    public List<Realm> initGetRealm() {
        List<Realm> realmList = new LinkedList<>();
        // ----- password
        //PasswordRealm passwordRealm = new PasswordRealm();
        //passwordRealm.setAccountProvider(accountProvider);
        //passwordRealm.setCredentialsMatcher(passwordMatcher);
        //passwordRealm.setAuthenticationTokenClass(PasswordToken.class);
        //realmList.add(passwordRealm);
        // ----- jwt
        JwtRealm jwtRealm = new JwtRealm(stringRedisTemplate,userMapper);
        // 改写shiro permiss匹配规则
        jwtRealm.setPermissionResolver(new CustomMatchPermissionResolver());
//        jwtRealm.setCredentialsMatcher(jwtMatcher);
        jwtRealm.setAuthenticationTokenClass(JwtToken.class);
        // 认证通过判断方式
        jwtRealm.setCredentialsMatcher(new CustomCredentialsMatcher());
        realmList.add(jwtRealm);
        return Collections.unmodifiableList(realmList);
    }
}
